package com.qipay.api.home.controller;

import com.google.zxing.WriterException;
import com.qipay.core.qrcode.QRCodeUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.net.URLDecoder;

/**
 * com.qipay.api.home.controller
 *
 * @author Administrator Ansen
 * @date 2017/10/30
 */

@RestController
@RequestMapping("/common")
public class CommonController {

    protected final Log logger = LogFactory.getLog(this.getClass());

    @RequestMapping(value = "/qrcode", method = RequestMethod.GET)
    public void createQRCode(
            @RequestParam @NotNull String content,
            @RequestParam Integer width,
            @RequestParam Integer height,
            HttpServletResponse httpResponse
    ) throws IOException, WriterException {
        if (width == null) {
            width = 300;
        }
        if (height == null) {
            height = 300;
        }

        httpResponse.addHeader("Pragma", "No-cache");
        httpResponse.setHeader("Cache-Control", "no-cache");
        httpResponse.setDateHeader("Expires", 0);
        httpResponse.setContentType("image/jpeg");

        String realContent = URLDecoder.decode(content, "utf8");
        QRCodeUtils.createQRCodeImageStream(content, width, height, "jpg", httpResponse.getOutputStream());
    }
}
